/**
 * Created by 1 on 2016/11/22.
 */
var numList = [];
var $ = function(name) {
    return document.querySelector(name);
}
/**
 * 跨浏览器兼容事件监听
 * @param element
 * @param type
 * @param handler
 */
function eventHandler(element,type,handler) {
    if (element.addEventListener){
        element.addEventListener(type,handler,false);
    }else if (element.attachEvent){
        element.attachEvent("on" + type,handler);
    }else {
        element["on" + type] = handler;
    }

}

/**
 * 将输入的内容插入数组
 */
function addArray() {
    var value = $("textarea").value;
    numList.unshift(value);
    renderList();
}
function queryArray() {
    var valueQuery = $("#queryInput").value;
    // var pattern = /valueQuery/g;
    for (var i = 0;i < numList.length;i++){
        if (valueQuery != null && valueQuery.length >0){
          numList[i].replace(new RegExp(valueQuery,'g'),"<span class='select'>"+valueQuery+"</span>");
        }
    }
    renderList();
}
/**
 * 图标渲染函数
 */
function renderList (){
    var number = " ";
    for(var i = 0;i < numList.length;i++){
        number += "<label class='squard'>"+numList[i]+"</label>";
    }
    $("#query-result").innerHTML = number;
}
/**
 * 初始化函数
 */
function init() {
    eventHandler($("#queryBtn"),'click',queryArray);
    eventHandler($("#addArray"),'click',addArray);
}
init();